package cn.kgc.tangcco.utils.qiniuyun;

import cn.hutool.setting.dialect.Props;
import cn.kgc.tangcco.model.ResultCode;
import com.alibaba.fastjson.JSON;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import com.qiniu.util.StringMap;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 李昊哲
 * @Description
 * @create 2020/9/6 下午3:58
 */
public class BaseQiniuyunUtil {
    private static Props props = new Props("qiniuyun.properties");
    private static String accessKey = props.getProperty("accessKey");
    private static String secretKey = props.getProperty("secretKey");
    private static String bucket = props.getProperty("bucket");
    private static String domain = props.getProperty("domain");
    // private static String region = ;
    // private static String region = ;
    private static Region region = null;

    static {
        switch (props.getProperty("region")) {
            case "z0":
                // 华东
                region = Region.region0();
                break;
            case "z1":
                // 华北
                region = Region.region1();
                break;
            case "z2":
                // 华南
                region = Region.region2();
                break;
            case "z3":
                // 北美
                region = Region.regionNa0();
                break;
            case "z4":
                //东南亚
                region = Region.regionAs0();
                break;
        }
    }

    /**
     * 上传管理对象
     */
    private static UploadManager uploadManager = new UploadManager(new Configuration(region));

    /**
     * @return 上传参数
     */
    public static QiniuyunUpload init() {
        return new QiniuyunUpload(accessKey, secretKey, bucket);
    }


    public static Map<String, Object> upload(InputStream inputStream) {
        QiniuyunUpload qiniuyunUpload = init();
        Map<String, Object> map = new HashMap<>();
        // 默认上传失败
        map.put("code", ResultCode.DATA_ERROR.getCode());
        map.put("msg", ResultCode.DATA_ERROR.getMsg());
        map.put("desc", ResultCode.DATA_ERROR.getDesc());
        // (被上传文件的btye数组,新文件名字, 上传凭证)
        Response resp = null;
        try {
            Response response = uploadManager.put(IOUtils.toByteArray(inputStream),accessKey,qiniuyunUpload.getToken());
            //解析上传成功的结果
            DefaultPutRet putRet = JSON.parseObject(response.bodyString(), DefaultPutRet.class);
            if (!StringUtils.isEmpty(putRet.key)) {
                // 访问地址 域名加图片名称 可以通过浏览器直接访问也可以在浏览器中引入
                String url = domain + putRet.key;
                // 上传成功
                map.put("code", ResultCode.SUCCESS.getCode());
                map.put("msg", ResultCode.SUCCESS.getMsg());
                map.put("desc", ResultCode.SUCCESS.getDesc());
                map.put("url", url);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            return map;
        }
    }

}
